package bgate.hung.activity;

import java.util.ArrayList;
import java.util.Vector;
import bgate.connector.DataTransfer;
import bgate.hung.workerclass.Table;
import bgate.hung.workerclass.TableListAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import bgate.hung.workerclass.TableRow;

public class ChoiseTable extends Activity {
	private static final String TAG = "ChoiseTableActivity";
	private ArrayList<TableRow> row = new ArrayList<TableRow>();
	private String account = "";
	private int[] us_array, sm_array, isPlay_array, se_array, mo_array,
			re_array;
	private String[] ro_array;
	private int numberTable;
	private DataTransfer dataTransfer = null;
	private IncommingMessage incommingMessage = null;
	private boolean isPushTask = false;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		Intent intent = getIntent();
		account = intent.getStringExtra("ACCOUNT");
		us_array = intent.getIntArrayExtra("US_ARRAY");
		sm_array = intent.getIntArrayExtra("SM_ARRAY");
		isPlay_array = intent.getIntArrayExtra("ISPLAY_ARRAY");
		se_array = intent.getIntArrayExtra("SE_ARRAY");

		dataTransfer = DataTransfer.getInstance();
		if (incommingMessage == null)
			incommingMessage = new IncommingMessage(dataTransfer.getOutQueue());
		if (!isPushTask && incommingMessage != null) {
			dataTransfer.putTask(incommingMessage);
			isPushTask = true;
		}

		setContentView(R.layout.choise_table_layout);

		numberTable = Math.min(Math.min(us_array.length, sm_array.length),
				Math.min(isPlay_array.length, se_array.length));
		int numberRow;
		if (numberTable % 3 == 0)
			numberRow = numberTable / 3;
		else
			numberRow = numberTable / 3 + 1;
		for (int i = 0; i < numberRow; i++) {
			Table table1, table2, table3;
			if (i * 3 < numberTable)
				table1 = new Table(i * 3, sm_array[i * 3], se_array[i * 3],
						us_array[i * 3], isPlay_array[i * 3]);
			else
				table1 = new Table(i * 3);

			if (i * 3 + 1 < numberTable)
				table2 = new Table(i * 3 + 1, sm_array[i * 3 + 1],
						se_array[i * 3 + 1], us_array[i * 3 + 1],
						isPlay_array[i * 3 + 1]);
			else
				table2 = new Table(i * 3 + 1);

			if (i * 3 + 2 < numberTable)
				table3 = new Table(i * 3 + 2, sm_array[i * 3 + 2],
						se_array[i * 3 + 2], us_array[i * 3 + 2],
						isPlay_array[i * 3 + 2]);
			else
				table3 = new Table(i * 3 + 2);

			TableRow tableRow = new TableRow(table1, table2, table3);
			row.add(tableRow);
		}

		ListView listView = (ListView) findViewById(R.id.listView_choise_table_layout);
		TableListAdapter adapter = new TableListAdapter(this,
				R.layout.table_listview_item, row);
		adapter.setNumberTable(numberTable);
		listView.setAdapter(adapter);
	}

	public void onResume() {
		super.onResume();
		dataTransfer = DataTransfer.getInstance();
		if (incommingMessage == null)
			incommingMessage = new IncommingMessage(dataTransfer.getOutQueue());
		if (!isPushTask && incommingMessage != null) {
			dataTransfer.putTask(incommingMessage);
			isPushTask = true;
		}
	}

	public void onPause() {
		super.onPause();
		if (isPushTask) {
			dataTransfer.removeTask(incommingMessage);
			isPushTask = false;
		}
		incommingMessage = null;
	}

	public void onBackPressed() {
		super.onBackPressed();
		dataTransfer.requestData("}RSE|&");
		finish();
	}

	public void sendRORequest(int id) {
		dataTransfer.requestData("}RO|" + Integer.valueOf(id) + "&");
	}
	
	private void enterGamePlay() {
		Intent intent = new Intent(this, PhomActivity.class);
		intent.putExtra("ACCOUNT", account);
		intent.putExtra("RO_ARRAY", ro_array);
		intent.putExtra("MO_ARRAY", mo_array);
		intent.putExtra("RE_ARRAY", re_array);
		startActivityForResult(intent, 0);
	}

	private class IncommingMessage implements Runnable {
		private final Vector mInQueue;

		public IncommingMessage(Vector inQueue) {
			this.mInQueue = inQueue;
		}

		public void run() {

			try {
				Thread.sleep(10);
				if (mInQueue.size() > 0) {

					String data = (String) mInQueue.elementAt(0);
					mInQueue.removeElementAt(0);
					Log.d(TAG, data);
					Vector<String> tempResultData = splitString(data, '}');
					for (int k = 0; k < tempResultData.size(); k++) {
						Vector<String> resultData = splitString(
								tempResultData.elementAt(k), '|');
						for (int i = 0; i < resultData.size(); i++) {
							String s = resultData.elementAt(i);
							if (s.equals("RO")) {
								ro_array = new String[resultData.size() - i - 1];
								int index = 0;
								for (int j = i + 1; j < resultData.size(); j++) {
									s = resultData.elementAt(j);
									ro_array[index] = s;
									index++;
								}
							}
							
							if (s.equals("MO")) {
								mo_array = new int[resultData.size() - i - 1];
								int index = 0;
								for (int j = i + 1; j < resultData.size(); j++) {
									s = resultData.elementAt(j);
									mo_array[index] = Integer.valueOf(s);
									index++;
								}
							}
							
							if (s.equals("RE")) {
								re_array = new int[resultData.size() - i - 1];
								int index = 0;
								for (int j = i + 1; j < resultData.size(); j++) {
									s = resultData.elementAt(j);
									re_array[index] = Integer.valueOf(s);
									index++;
								}
								enterGamePlay();
							}
						}
					}
				}
			} catch (InterruptedException ex) {
				Log.d(TAG, ex.toString());
			} catch (NullPointerException ex) {
				Log.d(TAG, ex.toString());
			}

		}
	}

	private Vector<String> splitString(String string, char delimiter) {
		Vector<String> mResult = new Vector<String>();
		TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(
				delimiter);

		// Once per string to split
		splitter.setString(string);
		for (String s : splitter) {
			String temp = "";
			for (int i = 0; i < s.length(); i++) {
				if (s.charAt(i) != '}' && s.charAt(i) != '&')
					temp += s.charAt(i);
			}
			mResult.addElement(temp);
		}

		return mResult;
	}
}
